package com.tjetc.controller;

import com.tjetc.common.Resp;
import com.tjetc.entity.User;
import com.tjetc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public Resp login(@RequestBody User user) {
        System.out.println(user);
        return userService.login(user);
    }

    @RequestMapping("register")
    public Resp register(@RequestBody User user){
        System.out.println(user);
        return userService.register(user);
    }

    @RequestMapping("edit/{option}")
    public Resp edit(@RequestBody User user, @PathVariable("option") String option){
        System.out.println(user);
        System.out.println(option);
        if(option.equals("delete"))
            return userService.delete(user.getId());
        else if(option.equals("add"))
            return userService.register(user);
        else return userService.edit(user);
    }

    @RequestMapping("getAll")
    public Resp getAll(){
        return userService.getAll();
    }

}
